package com.ruoyi.extension.jmliu1983.ds.dynamic;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.druid.DruidConfig;
import com.baomidou.dynamic.datasource.ds.ItemDataSource;
import com.ruoyi.framework.datasource.DynamicDataSource;
import com.baomidou.dynamic.datasource.ds.ItemDataSource;



public class DynamicDataSourceHelper {

	public final static void addDynamicRoutingDataSource(DynamicRoutingDataSource dynamicRoutingDataSource, DefaultDataSourceCreator creator, String dsName, String userName, String password, String jdbcUrl, String dbType) {
		
		if(dynamicRoutingDataSource!=null) {
			DruidConfig druidConfig = new DruidConfig();
			// 连接池核心参数
			druidConfig.setInitialSize(5);               // 初始连接数[8]
			druidConfig.setMaxActive(50);                // 最大活跃连接
			druidConfig.setMinIdle(5);                   // 最小空闲连接
			druidConfig.setMaxWait(60000);               // 获取连接超时时间

			DataSourceProperty property = new DataSourceProperty();
		    property.setPassword(password);
		    property.setUrl(jdbcUrl);
		    property.setUsername(userName);
		    property.setDriverClassName(dbType);
		    property.setDruid(druidConfig);		    	
		    dynamicRoutingDataSource.addDataSource(dsName, creator.createDataSource(property));
		}

	}
	public final static void addDynamicDataSource(DynamicDataSource dynamicRoutingDataSource, DefaultDataSourceCreator creator, String dsName, String userName, String password, String jdbcUrl, String dbType) {
//		TODO
		System.out.println(dynamicRoutingDataSource);

	}
	
	public final static List<JSONObject> getDynamicDruidDataSources(DynamicRoutingDataSource dynamicRoutingDataSource) {
		List<JSONObject> dbProperties = new ArrayList<JSONObject>();
		Map<String, DataSource> dsList = dynamicRoutingDataSource.getDataSources();
		dsList.forEach((dsName1, dsObj) -> {
			if (dsObj instanceof ItemDataSource) {
				ItemDataSource realDS = (ItemDataSource) dsObj;
				DataSource realDSFinal = realDS.getDataSource();

				if (realDSFinal instanceof DataSource) {
					DruidDataSource druidDS = (DruidDataSource) realDSFinal;
					String jdbcUrl = druidDS.getRawJdbcUrl();
					String dbType = druidDS.getDbType();
					String driverClassName = druidDS.getDriverClassName();
					JSONObject jobj = new JSONObject();
					jobj.put("jdbcUrl", jdbcUrl);
					jobj.put("dbType", dbType);
					jobj.put("driverClassName", driverClassName);
					jobj.put("dsName", dsName1);
					dbProperties.add(jobj);
				}
			}
		});		
	return dbProperties;	
	}
	public final static List<JSONObject> getDynamicDataSources(DynamicDataSource dataSource) {
		List<JSONObject> dbProperties = new ArrayList<JSONObject>();
//		DynamicDataSource dynamicDataSource = (DynamicDataSource)dataSource;
		Map<Object, DataSource> dslist = dataSource.getResolvedDataSources();
		JSONArray druidDSList = new JSONArray();
		dslist.forEach((name, ds)->{
			if(ds instanceof DruidDataSourceWrapper) {
				
				DruidDataSourceWrapper druidDataSourceWrapper  = (DruidDataSourceWrapper) ds;
				String jdbcUrl = druidDataSourceWrapper.getRawJdbcUrl();
				String dbType = druidDataSourceWrapper.getDbType();
				String dsName = druidDataSourceWrapper.getName();
				String driverClassName = druidDataSourceWrapper.getDriverClassName();
				JSONObject dsobj = new JSONObject();
				
				dsobj.put("jdbcUrl", jdbcUrl);
				dsobj.put("dbType", dbType);
				dsobj.put("driverClassName", driverClassName);
				dsobj.put("dsName", dsName);
				
				dbProperties.add(dsobj);
			}
		});
		return dbProperties;	
	}
}
